// $Id: UserNavigatorView.java 31 2011-09-21 17:36:34Z huijzer@gmail.com $
package lime.user.view;

import lime.common.LimeException;
import lime.model.User;
import lime.model.UserRoot;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;
import lime.user.editor.UserInput;
import lime.user.editor.UserPropertyEditor;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.ViewPart;

public class UserNavigatorView extends ViewPart {

	public static final String ID = "lime.user.view.navigator";

	// Services
	RepositoryService repositoryService;

	// Widgets
	ListViewer userViewer;

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");
	}

	@Override
	public void createPartControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(2, false));
		
		final UserFilter filter = new UserFilter();
		
		new Label(container, SWT.NONE).setText("User name contains:");
		
		Text containsText = new Text(container, SWT.BORDER);
		containsText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		containsText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent event) {
				Text source = (Text) event.getSource();
				String contains = source.getText();
				filter.setContains(contains);
				userViewer.refresh();
				updateStatusLine();
			}
		});
		
		userViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL);
		userViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		userViewer.setContentProvider(new BaseWorkbenchContentProvider());
		userViewer.setLabelProvider(new WorkbenchLabelProvider());
		userViewer.setInput(new UserRoot());

		userViewer.addFilter(filter);
		
		userViewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event) {
				ISelection selection = event.getSelection();
				if (selection != null && selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object element = structuredSelection.getFirstElement();
					if (element instanceof User) {
						User user = (User) element;
						UserInput userInput = new UserInput(user);
						try {
							getSite().getPage().openEditor(userInput, UserPropertyEditor.ID);
						} catch (PartInitException e) {
							throw new LimeException(e);
						}
					}
				}
			}
		});


		
		// Add context menu
		MenuManager menuManager = new MenuManager();
		menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		userViewer.getList().setMenu(menuManager.createContextMenu(userViewer.getList()));
		getSite().registerContextMenu(menuManager, userViewer);
		getSite().setSelectionProvider(userViewer);
	}

	@Override
	public void setFocus() {
		userViewer.getList().setFocus();
		updateStatusLine();
	}
	
	public void updateStatusLine() {
		int userCount = userViewer.getList().getItemCount();
		String message = "User count: " + userCount;
		getViewSite().getActionBars().getStatusLineManager().setMessage(message);
	}

}
